export function hexToCmyk(hexColor) {
  // Convert hex color to RGB
  let r = parseInt(hexColor.substring(1, 3), 16) / 255;
  let g = parseInt(hexColor.substring(3, 5), 16) / 255;
  let b = parseInt(hexColor.substring(5, 7), 16) / 255;

  // Convert RGB to CMYK
  let k = Math.min(1 - r, 1 - g, 1 - b);
  let c = (1 - r - k) / (1 - k) || 0;
  let m = (1 - g - k) / (1 - k) || 0;
  let y = (1 - b - k) / (1 - k) || 0;

  return [c, m, y, k];
}

export function rgbToCmyk(r, g, b) {
  // Normalize RGB values
  var r1 = r / 255;
  var g1 = g / 255;
  var b1 = b / 255;

  // Find the maximum of R, G, and B
  var k = 1 - Math.max(r1, g1, b1);

  // Calculate C, M, and Y
  var c = (1 - r1 - k) / (1 - k);
  var m = (1 - g1 - k) / (1 - k);
  var y = (1 - b1 - k) / (1 - k);
  return [c, m, y, k];
}

export function hexToRgb(hex) {
  // 去除 # 符号（如果存在）
  hex = hex.replace("#", "");

  // 将 HEX 转换为 RGB
  if (hex.length === 3) {
    hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
  }
  let r = parseInt(hex.substring(0, 2), 16);
  let g = parseInt(hex.substring(2, 4), 16);
  let b = parseInt(hex.substring(4, 6), 16);

  // 返回 RGB 值
  return [r, g, b];
}

// 将 RGB 转换为 HEX
export function rgbToHex(r, g, b) {
  // 使用位运算将 RGB 转换为 HEX
  let hex = ((r << 16) | (g << 8) | b).toString(16);

  // 如果 HEX 不足 6 位，则在前面补 0
  while (hex.length < 6) {
    hex = "0" + hex;
  }

  // 添加 # 符号
  hex = "#" + hex;

  // 返回 HEX 值
  return hex;
}

export function isHexColor(str) {
  // 使用正则表达式匹配十六进制颜色的格式
  var regex = /^#?([0-9A-F]{3}){1,2}$/i;

  // 判断字符串是否匹配正则表达式
  return regex.test(str);
}

